package org.ens.delegation.dao.impl;

import java.util.List;
import org.ens.delegation.bo.Agent;
import org.ens.delegation.dao.IAgentDao;
import org.hibernate.Criteria;
import org.hibernate.criterion.*;

public class AgentDao extends GenericDao<Agent, Integer> implements IAgentDao{

	@Override
	public Class<Agent> getReferencedClass() {
		return Agent.class;
	}

    @Override
    public List<Agent> findByFilters(String orderCol,String orderDir,String numDoti,String cin, String nomFr, String nomAr, String dateNaiss, String dateTit, String etablissement) {
        
        Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(getReferencedClass());
        
        Order o ;
        if(orderDir.equals("asc"))
            o = Order.asc(orderCol);
        else o = Order.desc(orderCol);
        
        if(numDoti != null && !numDoti.isEmpty())
            criteria.add(Restrictions.eq("id", Integer.valueOf(numDoti)));
        
        if(nomFr != null && !nomFr.isEmpty())
            criteria.add(Restrictions.like("nomFR", nomFr, MatchMode.ANYWHERE));
        
        if(nomAr != null && !nomAr.isEmpty())
            criteria.add(Restrictions.like("nomAR", nomAr, MatchMode.ANYWHERE));
        
        if(cin != null && !cin.isEmpty())
            criteria.add(Restrictions.eq("cin", cin));
        
        if(dateNaiss != null && !dateNaiss.isEmpty())
            criteria.add(Restrictions.eq("dateNaiss", dateNaiss));
               
        if(dateTit != null && !dateTit.isEmpty())
            criteria.add(Restrictions.eq("dateTit", dateTit));
            
        if(etablissement != null && !etablissement.isEmpty()){
            criteria.createAlias("etablissementAffecte", "ef", CriteriaSpecification.LEFT_JOIN);
            criteria.add(Restrictions.like("ef.nomFR", etablissement, MatchMode.ANYWHERE));
        }

        criteria.addOrder(o);
        
        return criteria.list();
    }

    @Override
    public List<Agent> findByFiltersWithFirstMaxResult(int first, int max,String orderCol,String orderDir,String numDoti,String cin, String nomFr, String nomAr, String dateNaiss, String dateTit, String etablissement) {

        Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(getReferencedClass());
        
        Order o = null;
        if(orderDir.equals("asc"))
            o = Order.asc(orderCol);
        else o = Order.desc(orderCol);
        
        if(numDoti != null && !numDoti.isEmpty())
            criteria.add(Restrictions.eq("id", Integer.valueOf(numDoti)));
        
        if(nomFr != null && !nomFr.isEmpty())
            criteria.add(Restrictions.like("nomFR", nomFr, MatchMode.ANYWHERE));
        
        if(nomAr != null && !nomAr.isEmpty())
            criteria.add(Restrictions.like("nomAR", nomAr, MatchMode.ANYWHERE));
        
        if(cin != null && !cin.isEmpty())
            criteria.add(Restrictions.eq("cin", cin));
        
        if(dateNaiss != null && !dateNaiss.isEmpty())
            criteria.add(Restrictions.eq("dateNaiss", dateNaiss));
        
        if(dateTit != null && !dateTit.isEmpty())
            criteria.add(Restrictions.eq("dateTit", dateTit));
        
        if(etablissement != null && !etablissement.isEmpty()){
            criteria.createAlias("etablissementAffecte", "ef", CriteriaSpecification.LEFT_JOIN);
            criteria.add(Restrictions.like("ef.nomFR", etablissement, MatchMode.ANYWHERE));
        }
        
        criteria.setFirstResult(first);
        criteria.setMaxResults(max);
        criteria.addOrder(o);
        return criteria.list();
    }

    @Override
    public Integer getRowCountByFilters(String numDoti,String cin, String nomFr, String nomAr, String dateNaiss, String dateTit, String etablissement) {
        
        Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(getReferencedClass());
        
        if(numDoti != null && !numDoti.isEmpty())
            criteria.add(Restrictions.eq("id", Integer.valueOf(numDoti)));
        if(nomFr != null && !nomFr.isEmpty())
            criteria.add(Restrictions.like("nomFR", nomFr, MatchMode.ANYWHERE));
        
        if(nomAr != null && !nomAr.isEmpty())
            criteria.add(Restrictions.like("nomAR", nomAr, MatchMode.ANYWHERE));
        
        if(cin != null && !cin.isEmpty())
            criteria.add(Restrictions.eq("cin", cin));
        
        if(dateNaiss != null && !dateNaiss.isEmpty())
            criteria.add(Restrictions.eq("dateNaiss", dateNaiss));
        
        if(dateTit != null && !dateTit.isEmpty())
            criteria.add(Restrictions.eq("dateTit", dateTit));
             
        if(etablissement != null && !etablissement.isEmpty()){
            criteria.createAlias("etablissementAffecte", "ef", CriteriaSpecification.LEFT_JOIN);
            criteria.add(Restrictions.like("ef.nomFR", etablissement, MatchMode.ANYWHERE));
        }
        
        criteria.setProjection(Projections.rowCount());
        
        return (Integer) criteria.uniqueResult();
    }


}
